package com.qh.org.service.impl;

import com.qh.basic.exception.BusinessException;
import com.qh.basic.service.impl.BaseServiceImpl;
import com.qh.basic.util.BaiduAiUtil;
import com.qh.basic.util.Md5Utils;
import com.qh.basic.util.StrUtils;
import com.qh.org.domain.Employee;
import com.qh.org.domain.Shop;
import com.qh.org.domain.ShopAuditLog;
import com.qh.org.dto.ShopDto;
import com.qh.org.mapper.EmployeeMapper;
import com.qh.org.mapper.ShopAuditLogMapper;
import com.qh.org.mapper.ShopMapper;
import com.qh.org.service.IShopService;
import com.qh.user.domain.Logininfo;
import com.qh.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    /* 发送邮件的 */
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) throws MessagingException {
        //一.校验
        //1.空值校验
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("信息不能为空！！！");
        }
        //2.两次输入密码是否一致校验
        if (!shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new BusinessException("两次密码输入不一致！！！");
        }
        //3.当前店铺是否入驻过校验
        Shop shopTem = shopMapper.findByNameAndAddress(shop);
        if (shopTem != null){
            throw new BusinessException("店铺已入驻，请直接登录，忘记密码请联系管理员！！！");
        }

        //二.百度AI智能审核【店铺名字，logo是否合法】
        if (!BaiduAiUtil.TextCensor(shop.getName())){
            throw new BusinessException("店铺名字不合规！！！");
        }
        if (!BaiduAiUtil.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺Logo不合规！！！");
        }

        //三.添加到数据库
        //1.先添加员工信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.save(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.save(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShop_id(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.该状态：1待审核  2审核通过
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1);//[先写死]
        log.setAuditId(350l);//[先写死]
        shopAuditLogMapper.save(log);
        //3.发送邮件通知店长去激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1638857628@qq.com");
        helper.setSubject("店铺激活邮件");
        //先查出要审核的店铺出来
        Shop shop = shopMapper.loadOne(log.getShopId());
        //true - 能够再内容中编写html标签 - 复杂邮件会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()+"' ><a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);
        //收件人
        //查出来
        Employee employee = employeeMapper.loadOne(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditReject(ShopAuditLog log) throws MessagingException {
        //1.该状态：1待审核  4审核驳回
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);//[先写死]
        log.setAuditId(350l);//[先写死]

        //3.发送邮件通知店长修改入驻信息
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("659769497@qq.com");
        helper.setSubject("店铺激活失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://127.0.0.1:8081/#/regiseterEdit/"+log.getShopId()
                +"'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //店铺管理员
        Shop shop = shopMapper.loadOne(log.getShopId());
        Employee admin = employeeMapper.loadOne(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void updateById(Long id) {
        shopMapper.updateState(3,id);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
